package org.vnl.platform.dk.util;

import java.util.HashMap;
import java.util.Hashtable;

import org.apache.log4j.Logger;

/**
 * 主要作用是讓應用程式只有單一存取設定的地方.
 * 
 * @author Vaughan
 *
 */
public class APContext {

    private static final Logger logger = Logger.getLogger(APContext.class);
    
    private static APContext instance;
    
    private HashMap<String, Object> configure;
    private HashMap<String, String> strConfigure;

    private static final ThreadLocal<Hashtable<String, Object>> currentThreadSetting = new ThreadLocal<Hashtable<String,Object>>();
    
    private APContext() {
        configure = new HashMap<String, Object>();
        strConfigure = new HashMap<String, String>();
        currentThreadSetting.set(new Hashtable<String, Object>());
    }
    
    /**
     * 於同一個處理thread中,某些需要共同使用的參數可從此儲存
     * 
     * @param key
     * @param value
     */
    public void setCurrentThreadSetting(String key, Object value) {
        currentThreadSetting.get().put(key, value);
    }
    
    /**
     * 於同一個處理thread中,某些需要共同使用的參數可從此取出
     * 
     * @param key
     * @return
     */
    public Object getCurrentThreadSetting(String key) {
       return currentThreadSetting.get().get(key); 
    }
    
    public static APContext getInstance() {
        if (instance == null) {
            instance = new APContext();
        }
        return instance;
    }

    public Object getConfigure(String key) {
        return configure.get(key);
    }    
    
    public void setConfigure(String key, Object value) {
        logger.debug("Add Configure key="+key+":value="+value);
        configure.put(key, value);
    }
    
    public String getStringConfigure(String key) {
        return strConfigure.get(key);
    }
    
    public void setStringConfigure(String key, String value) {
        logger.debug("Add Configure key="+key+":value="+value);
        strConfigure.put(key, value);
    }
    
    public void removeStringConfigure(String key) {
        strConfigure.remove(key);
    }
    
    public void removeConfigure(String key) {
        configure.remove(key);
    }
    
}
